package com.blb.controller;


import com.blb.entity.Department;
import com.blb.entity.Post;
import com.blb.service.IDepartmentService;
import com.blb.utils.ResponseHelp;
import com.blb.utils.ShiroUtils;
import com.blb.utils.UUIDUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;

import java.lang.reflect.Array;
import java.util.*;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author HuaHao
 * @since 2019-11-14
 */
@Controller
@RequestMapping("/department")
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    /*跳转部门管理页面*/
    @RequiresPermissions("personal_department")
    @GetMapping("")
    public String department(){
        return "personal/department";
    }

    /*根据公司id查到所有部门*/
    @RequiresPermissions("personal_department")
    @ResponseBody
    @RequestMapping("getAllDepartment")
    public ResponseHelp getAll(){
        String cId = ShiroUtils.getUser().getCompanyId();
        List<Department> list = departmentService.getAllDepartment(cId);
        ResponseHelp responseHelp = new ResponseHelp();
        responseHelp.setData(list);
        return responseHelp;
    }

    /*根据部门id查部门信息*/
    @RequiresPermissions("personal_department")
    @ResponseBody
    @RequestMapping("getById")
    public ResponseHelp getDepartmentById(String id){
        Department department = departmentService.getDepartmentById(id);
        ResponseHelp responseHelp = new ResponseHelp();
        responseHelp.setData(department);
        return responseHelp;
    }

    /*更新部门信息*/
    @RequiresPermissions("personal_department")
    @ResponseBody
    @RequestMapping("update")
    public ResponseHelp updateDepartment(@RequestBody Department department){
        //清除名字为空的职位
        List<Post> list = new ArrayList<Post>();
        for(Post post : department.getPosts()){
            if(!StringUtils.isBlank(post.getName())){
                list.add(post);
            }
        }
        department.setPosts(list);
        departmentService.updateDepartment(department);
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("msg","删除成功");
        map.put("rs",true);
        ResponseHelp responseHelp = new ResponseHelp();
        responseHelp.setData(map);
        return responseHelp;
    }

    /*添加部门*/
    @RequiresPermissions("personal_department")
    @ResponseBody
    @RequestMapping("insert")
    public ResponseHelp insertDepartment(@RequestBody Department department){
        //加入id值
        department.setId(UUIDUtils.getId());
        department.setCId(ShiroUtils.getUser().getCompanyId());
        //判断是否有pid，没有就赋值为0
        String pid = department.getPid() == null || department.getPid() == "" ? "0" : department.getPid();
        department.setPid(pid);
        departmentService.save(department);
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("msg","添加成功");
        map.put("rs",true);
        ResponseHelp responseHelp = new ResponseHelp();
        responseHelp.setData(map);
        return responseHelp;
    }

    /*删除部门*/
    @RequiresPermissions("personal_department")
    @ResponseBody
    @RequestMapping("del")
    public ResponseHelp delete(String id){
        //查询是否有子部门
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("pid",id);
        Collection<Department> list = departmentService.listByMap(map);
        //设置返回数据
        Map<String,Object> map2 = new HashMap<String,Object>();
        map2.put("msg","删除成功");
        map2.put("count",1);
        //当有子部门时返回true，没有时返回false并执行删除
        if(list.size() > 0){
            map2.put("rs",true);
        }else{
            map2.put("rs",false);
            departmentService.deleteDepartmentById(id);
        }
        ResponseHelp responseHelp = new ResponseHelp();
        responseHelp.setData(map2);
        return responseHelp;
    }

    /*判断部门编号是否重复*/
    @RequiresPermissions("personal_department")
    @ResponseBody
    @RequestMapping("testNo")
    public ResponseHelp testNo(String no,String id){
        String companyId = ShiroUtils.getUser().getCompanyId();
        //设置返回数据格式
        Map<String,Object> map2 = new HashMap<String,Object>();
        map2.put("count",1);
        //根据编号查的查询条件
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("no", no);
        map.put("c_id",companyId);
        Collection<Department> list = departmentService.listByMap(map);
        //当没有数据时
        if(StringUtils.isBlank(id)){
            //没查出来时返回false
            if(list.size() == 0){
                map2.put("rs",false);
            }else{
                map2.put("rs",true);
            }
        }else {
            //查到id对应的部门
            Department department = departmentService.getById(id);
            //判断部门的编号是否修改,并且根据编号能查到数据
            if(!department.getNo().equals(no) && list.size() != 0){
                map2.put("rs",true);
            }else {
                map2.put("rs",false);
            }
        }
        ResponseHelp responseHelp = new ResponseHelp();
        responseHelp.setData(map2);
        return responseHelp;
    }
}

